package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sun.swing.StringUIClientPropertyKey;

import java.awt.*;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-26
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    //登录的方法
    @Override
    public String login(UcenterMember member) {
        //获取登录手机号和mm
        String mobile = member.getMobile();
        String password = member.getPassword();
        //手机号和密码的非空判断
        if(StringUtils.isEmpty(member) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登陆失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(mobileMember == null) {
            //没有这个手机号
            throw new GuliException(20001, "手机号不存在");
        }

        //判断密码
        //存储在数据库中的密码是加密的
        //先加密 再比较 MD5加密
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            System.out.println(MD5.encrypt(password));
            throw new GuliException(20001, "密码错误");
        }

        //判断是否禁用
        if(mobileMember.getIsDisabled()) {
            throw new GuliException(20001, "用户被禁用");
        }
        //登陆成功
        //生成token字符串
        return JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
    }

    //注册的方法
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "注册失败");
        }

        //判断验证码
        //获取redis里面的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)) {
            throw new GuliException(20001, "验证码错误");
        }

        //判断手机号是否重复，表里面存在相同的手机号，就不添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0) {
            throw new GuliException(20001, "用户已经存在");
        }

        //添加到数据中
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://edu-545466093.oss-cn-beijing.aliyuncs.com/2021/09/29/dcade2606010411c8a1f6c8bf1a2d5882.jpg");
        baseMapper.insert(member);
    }

    //查询一天的注册人数
    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
